// $codepro.audit.disable numericLiterals
/**
 * uses view package
 */
package javamonsters.view;

import javax.swing.JPanel;
import javax.swing.DefaultListModel;
import javax.swing.JScrollPane;
import javax.swing.JButton;
import javax.swing.JList;
import javax.swing.JLabel;
import javax.swing.SwingConstants;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Font;
import java.awt.FontFormatException;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.io.File;
import java.io.IOException;
import java.awt.Color;

/**
 * This is the view for the Marketplace screens
 * 
 * @author Evan LaHurd
 * @version 1.0
 */
public class MarketplaceView extends JPanel {
	
	/**
	 * JButtons
	 */
	private final JButton toCargoButton, toMarketButton, backToMapButton;

	/**
	 * Scroll pane in marketplace view 
	 */
	private final JScrollPane marketScrollPane;
	
	/**
	 * default list models for buy/sell
	 */
	private final DefaultListModel buyListModel, sellListModel;
	
	/**
	 * JLists for buy/sell
	 */
	private final JList buyList, sellList;
	
	/**
	 * tech level label
	 */
	private final JLabel lblTechLevel;
	
	/**
	 * system label
	 */
	private final JLabel lblSystemsTl;
	
	/**
	 * avail credits label
	 */
	private final JLabel lblAvailableCredits;
	
	/**
	 * credits label
	 */
	private final JLabel creditsLabel;
	
	/**
	 * system label
	 */
	private final JLabel systemLabel;
	
	/**
	 * market inventory label
	 */
	private final JLabel marketInventoryLabel;
	
	/**
	 * cargo inventory label
	 */
	private final JLabel cargoInventoryLabel;
	
	/**
	 * go to ship yard button
	 */
	private final JButton toShipYardButton;
	
	/**
	 * fonts
	 */
	private final Font listTitleFont, headerFont, infoFont, buttonFont;
	
	/**
	 * Constructor for MarketplaceView which adds all of the appropriate components
	 * @throws IOException 
	 * @throws FontFormatException 
	 */
	public MarketplaceView() throws FontFormatException, IOException {
		super.setLayout(new GridLayout(1, 3, 0, 0));
		super.setPreferredSize(new Dimension(700, 380));
		
		final File fontFile = new File("Fonts/Compass.ttf");
		final Font font = Font.createFont(Font.TRUETYPE_FONT, fontFile);
		headerFont = font.deriveFont(18f);
		listTitleFont = font.deriveFont(14f);
		infoFont = font.deriveFont(10f);
		buttonFont = font.deriveFont(12f);
		
		final JPanel marketPanel = new JPanel();
		marketPanel.setBackground(Color.BLACK);
		super.add(marketPanel);
		marketPanel.setLayout(new GridLayout(1, 1, 0, 0));
		
		marketScrollPane = new JScrollPane();
		marketPanel.add(marketScrollPane);
		
		buyListModel = new DefaultListModel();
		buyList = new JList(buyListModel);
		buyList.setBackground(Color.BLACK);
		buyList.setForeground(Color.WHITE);
		buyList.setFont(buttonFont);
		marketScrollPane.setViewportView(buyList);
		
		marketInventoryLabel = new JLabel("Market Inventory");
		marketInventoryLabel.setHorizontalAlignment(SwingConstants.CENTER);
		marketInventoryLabel.setFont(listTitleFont);
		marketScrollPane.setColumnHeaderView(marketInventoryLabel);
		
		final JPanel buttonPanel = new JPanel();
		buttonPanel.setBackground(Color.BLACK);
		super.add(buttonPanel);
		final GridBagLayout gbl_buttonPanel = new GridBagLayout();
		gbl_buttonPanel.columnWidths = new int[]{116, 111, 19};
		gbl_buttonPanel.rowHeights = new int[]{60, 60, 40, 40, 60, 0, 0, 0, 0};
		gbl_buttonPanel.columnWeights = new double[]{0.0, 0.0, Double.MIN_VALUE};
		gbl_buttonPanel.rowWeights = new double[]{
				0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
		buttonPanel.setLayout(gbl_buttonPanel);
		
		//Displays the current system the Player is in
		systemLabel = new JLabel("System");
		systemLabel.setForeground(Color.WHITE);
		systemLabel.setHorizontalAlignment(SwingConstants.CENTER);
		systemLabel.setFont(headerFont);
		final GridBagConstraints gbc_systemLabel = new GridBagConstraints();
		gbc_systemLabel.gridwidth = 2;
		gbc_systemLabel.fill = GridBagConstraints.BOTH;
		gbc_systemLabel.insets = new Insets(0, 0, 5, 0);
		gbc_systemLabel.gridx = 0;
		gbc_systemLabel.gridy = 0;
		buttonPanel.add(systemLabel, gbc_systemLabel);
		
		final JLabel lblMarketplace = new JLabel("Marketplace");
		lblMarketplace.setForeground(Color.WHITE);
		lblMarketplace.setHorizontalAlignment(SwingConstants.CENTER);
		lblMarketplace.setFont(headerFont);
		final GridBagConstraints gbc_lblMarketplace = new GridBagConstraints();
		gbc_lblMarketplace.gridwidth = 2;
		gbc_lblMarketplace.fill = GridBagConstraints.BOTH;
		gbc_lblMarketplace.insets = new Insets(0, 0, 5, 0);
		gbc_lblMarketplace.gridx = 0;
		gbc_lblMarketplace.gridy = 1;
		buttonPanel.add(lblMarketplace, gbc_lblMarketplace);
		
		//displays "Tech Level: "
		lblTechLevel = new JLabel("Tech Level:");
		lblTechLevel.setForeground(Color.WHITE);
		lblTechLevel.setFont(infoFont);
		lblTechLevel.setHorizontalAlignment(SwingConstants.CENTER);
		final GridBagConstraints gbc_lblTechLevel = new GridBagConstraints();
		gbc_lblTechLevel.anchor = GridBagConstraints.WEST;
		gbc_lblTechLevel.fill = GridBagConstraints.VERTICAL;
		gbc_lblTechLevel.insets = new Insets(0, 0, 5, 5);
		gbc_lblTechLevel.gridx = 0;
		gbc_lblTechLevel.gridy = 2;
		buttonPanel.add(lblTechLevel, gbc_lblTechLevel);
		
		//displays the System's tech level
		lblSystemsTl = new JLabel("System's TL");
		lblSystemsTl.setForeground(Color.WHITE);
		lblSystemsTl.setFont(infoFont);
		lblSystemsTl.setHorizontalAlignment(SwingConstants.CENTER);
		final GridBagConstraints gbc_lblSystemsTl = new GridBagConstraints();
		gbc_lblSystemsTl.anchor = GridBagConstraints.WEST;
		gbc_lblSystemsTl.fill = GridBagConstraints.VERTICAL;
		gbc_lblSystemsTl.insets = new Insets(0, 0, 5, 0);
		gbc_lblSystemsTl.gridx = 1;
		gbc_lblSystemsTl.gridy = 2;
		buttonPanel.add(lblSystemsTl, gbc_lblSystemsTl);
		
		//displays "Available credits: "
		lblAvailableCredits = new JLabel("Available credits:");
		lblAvailableCredits.setForeground(Color.WHITE);
		lblAvailableCredits.setFont(infoFont);
		lblAvailableCredits.setHorizontalAlignment(SwingConstants.CENTER);
		final GridBagConstraints gbc_lblAvailableCredits = new GridBagConstraints();
		gbc_lblAvailableCredits.anchor = GridBagConstraints.WEST;
		gbc_lblAvailableCredits.fill = GridBagConstraints.VERTICAL;
		gbc_lblAvailableCredits.insets = new Insets(0, 0, 5, 5);
		gbc_lblAvailableCredits.gridx = 0;
		gbc_lblAvailableCredits.gridy = 3;
		buttonPanel.add(lblAvailableCredits, gbc_lblAvailableCredits);
		
		//dispalys the available credits the Player has
		creditsLabel = new JLabel("Credits");
		creditsLabel.setForeground(Color.WHITE);
		creditsLabel.setFont(infoFont);
		creditsLabel.setHorizontalAlignment(SwingConstants.CENTER);
		final GridBagConstraints gbc_creditsLabel = new GridBagConstraints();
		gbc_creditsLabel.anchor = GridBagConstraints.WEST;
		gbc_creditsLabel.fill = GridBagConstraints.VERTICAL;
		gbc_creditsLabel.insets = new Insets(0, 0, 5, 0);
		gbc_creditsLabel.gridx = 1;
		gbc_creditsLabel.gridy = 3;
		buttonPanel.add(creditsLabel, gbc_creditsLabel);
		//button that adds marketplace items to Player's cargo hold
		toCargoButton = new JButton("BUY");
		toCargoButton.setFont(buttonFont);
		final GridBagConstraints gbc_toCargoButton = new GridBagConstraints();
		gbc_toCargoButton.fill = GridBagConstraints.BOTH;
		gbc_toCargoButton.insets = new Insets(0, 0, 5, 5);
		gbc_toCargoButton.gridx = 0;
		gbc_toCargoButton.gridy = 4;
		buttonPanel.add(toCargoButton, gbc_toCargoButton);
		
		//button that adds Player's items to the marketplace
		toMarketButton = new JButton("SELL");
		toMarketButton.setFont(buttonFont);
		final GridBagConstraints gbc_toMarketButton = new GridBagConstraints();
		gbc_toMarketButton.fill = GridBagConstraints.BOTH;
		gbc_toMarketButton.insets = new Insets(0, 0, 5, 0);
		gbc_toMarketButton.gridx = 1;
		gbc_toMarketButton.gridy = 4;
		buttonPanel.add(toMarketButton, gbc_toMarketButton);
		
		toShipYardButton = new JButton("To Ship Yard");
		toShipYardButton.setFont(buttonFont);
		final GridBagConstraints gbc_toShipYardButton = new GridBagConstraints();
		gbc_toShipYardButton.fill = GridBagConstraints.HORIZONTAL;
		gbc_toShipYardButton.gridwidth = 2;
		gbc_toShipYardButton.insets = new Insets(0, 0, 5, 5);
		gbc_toShipYardButton.gridx = 0;
		gbc_toShipYardButton.gridy = 5;
		buttonPanel.add(toShipYardButton, gbc_toShipYardButton);
		
		//button that takes Player back to the Universe Map screen
		backToMapButton = new JButton("Back To Universe Map");
		backToMapButton.setFont(buttonFont);
		final GridBagConstraints gbc_backToMapButton = new GridBagConstraints();
		gbc_backToMapButton.insets = new Insets(0, 0, 5, 0);
		gbc_backToMapButton.gridwidth = 2;
		gbc_backToMapButton.fill = GridBagConstraints.BOTH;
		gbc_backToMapButton.gridx = 0;
		gbc_backToMapButton.gridy = 6;
		buttonPanel.add(backToMapButton, gbc_backToMapButton);
		
		final JPanel cargoPanel = new JPanel();
		super.add(cargoPanel);
		cargoPanel.setLayout(new GridLayout(1, 1, 0, 0));
		
		final JScrollPane cargoScrollPane = new JScrollPane();
		cargoPanel.add(cargoScrollPane);
		
		sellListModel = new DefaultListModel();
		sellList = new JList(sellListModel);
		sellList.setBackground(Color.BLACK);
		sellList.setForeground(Color.WHITE);
		sellList.setFont(buttonFont);
		cargoScrollPane.setViewportView(sellList);
		
		cargoInventoryLabel = new JLabel("Cargo Inventory");
		cargoInventoryLabel.setHorizontalAlignment(SwingConstants.CENTER);
		cargoInventoryLabel.setFont(listTitleFont);
		cargoScrollPane.setColumnHeaderView(cargoInventoryLabel);

	}
	
	/**
	 * getter for buy button
	 * 
	 * @return button that allows player to purchase items
	 */
	public JButton getToCargoButton() {
		return toCargoButton;
	}
	
	/**
	 * getter for sell button
	 * 
	 * @return button that allows player to sell items
	 */
	public JButton getToMarketButton() {
		return toMarketButton;
	}
	
	/**
	 * getter for back button
	 * 
	 * @return button that allows player to return to the Universe Map
	 */
	public JButton getBackToMapButton() {
		return backToMapButton;
	}
	
	/**
	 * getter for button that takes player to the Ship Yard
	 * 
	 * @return button that allows player to travel to the Ship Yard
	 */
	public JButton getToShipYardButton() {
		return toShipYardButton;
	}
	
	/**
	 * getter for the list model that the buy list uses
	 * 
	 * @return the DefaultListModel that the buy list uses to display data
	 */
	public DefaultListModel getBuyListModel() {
		return buyListModel;
	}
	
	/**
	 * getter for the list model that the sell list uses
	 * 
	 * @return the DefaultListModel that the sell list uses to display data
	 */
	public DefaultListModel getSellListModel() {
		return sellListModel;
	}
	
	/**
	 * getter for the JList that displays purchasable items
	 * 
	 * @return the JList that displays items that the Player can buy
	 */
	public JList getBuyList() {
		return buyList;
	}
	
	/**
	 * getter for the JList that displays sellable items
	 * 
	 * @return the JList that displays items that the Player can sell
	 */
	public JList getSellList() {
		return sellList;
	}
	
	/**
	 * getter for the Tech Level label
	 * 
	 * @return the JLabel that displays the tech level of the system
	 */
	public JLabel getTechLevelLabel() {
		return lblSystemsTl;
	}
	
	/**
	 * getter for the system name label
	 * 
	 * @return the JLabel that displays the name of the system
	 */
	public JLabel getSystemLabel() {
		return systemLabel;
	}
	
	/**
	 * getter for the credits label
	 * 
	 * @return the JLabel that displays the available credits the Player has
	 */
	public JLabel getCreditsLabel() {
		return creditsLabel;
	}
}
